package zhjb.platform.module.schedule.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import zhjb.platform.module.schedule.bean.DateBlock;
import zhjb.platform.module.schedule.bean.MDate;
import zhjb.platform.module.schedule.bean.ScheduleTask;
import zhjb.platform.module.schedule.service.ScheduleService;
import zhjb.platform.util.SpringUtil;
import zhjb.platform.web.BaseAction;
import zhjb.platform.web.module.system.bean.User;

import com.opensymphony.xwork2.Action;

public class ScheduleAction extends BaseAction {
	private ScheduleService scheduleService = (ScheduleService) SpringUtil.getWebApplicationContext().getBean("scheduleService");
	private List<DateBlock> dataBlocks = null;
	private int year;
	private int month;
	private MDate nextYear;
	private MDate nextMonth;
	private MDate preYear;
	private MDate preMonth;
	private ScheduleTask task;
	private String message;
	private List<ScheduleTask> taskList;
	private String taskIds;
	private String deleteId;

	/**
	 * 进入首页
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String index() throws ParseException {
		Calendar calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH);
		schedule();
		return Action.SUCCESS;
	}

	/**
	 * 根据年月查看
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String schedule() throws ParseException {
		Calendar calendar = Calendar.getInstance();
		int curdate = calendar.get(Calendar.DAY_OF_MONTH);
		int curyear = calendar.get(Calendar.YEAR);
		int curmonth = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);

		calendar.add(Calendar.YEAR, -1);
		preYear = new MDate();
		preYear.setYear(calendar.get(Calendar.YEAR));
		preYear.setMonth(month);
		calendar.add(Calendar.YEAR, 1);

		calendar.add(Calendar.MONTH, -1);
		preMonth = new MDate();
		preMonth.setMonth(calendar.get(Calendar.MONTH));
		preMonth.setYear(calendar.get(Calendar.YEAR));
		calendar.add(Calendar.MONTH, 1);

		calendar.add(Calendar.YEAR, 1);
		nextYear = new MDate();
		nextYear.setYear(calendar.get(Calendar.YEAR));
		nextYear.setMonth(calendar.get(Calendar.MONTH));
		calendar.add(Calendar.YEAR, -1);

		calendar.add(Calendar.MONTH, 1);
		nextMonth = new MDate();
		nextMonth.setYear(calendar.get(Calendar.YEAR));
		nextMonth.setMonth(calendar.get(Calendar.MONTH));
		calendar.add(Calendar.MONTH, -1);

		List<MDate> mDateList = scheduleService.getAllDateInAMonth(year, month);
		User user = (User) request.getSession().getAttribute("user");
		scheduleService.findTaskByDay(mDateList, user);
		dataBlocks = scheduleService.getDateBlock(mDateList);
		for (int i = 0; i < dataBlocks.size(); i++) {
			if (dataBlocks.get(i).getDate() != null && dataBlocks.get(i).getDate().getYear() == curyear && dataBlocks.get(i).getDate().getMonth() == (curmonth + 1) && dataBlocks.get(i).getDate().getDate() == curdate) {
				dataBlocks.get(i).getDate().setCurDate(true);
			}
		}
		return Action.SUCCESS;
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @return
	 */
	public String toAdd() {
		return Action.SUCCESS;
	}

	public String addTask() throws ParseException {
		if (task == null || task.getTitle() == null || task.getTitle().trim().length() == 0 || task.getStart() == null || task.getEnd() == null) {
			message = "数据输入不完整";
			return Action.INPUT;
		} else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			task.setStart(sdf1.parse(sdf.format(task.getStart()) + " 00:00:00"));
			task.setEnd(sdf1.parse(sdf.format(task.getEnd()) + " 23:59:59"));
			User user = (User) request.getSession(false).getAttribute("user");
			task.setUser(user != null ? user.getId() : "1");
			if (task.getEnd().getTime() <= task.getStart().getTime()) {
				message = "时间输入错误";
				return Action.INPUT;
			}
			scheduleService.addTask(task);
		}
		return Action.SUCCESS;
	}

	/**
	 * 找到任务
	 * 
	 * @return
	 */
	public String findTask() {
		if (taskIds != null) {
			String[] taskIdArray = taskIds.split(",");
			Set<String> taskIds = new HashSet<String>(Arrays.asList(taskIdArray));
			if (deleteId != null) {
				taskIds.remove(deleteId);
			}
			taskList = scheduleService.deleteTaskId(taskIds, deleteId);
		}
		return Action.SUCCESS;
	}

	public ScheduleService getScheduleService() {
		return scheduleService;
	}

	public void setScheduleService(ScheduleService scheduleService) {
		this.scheduleService = scheduleService;
	}

	public List<DateBlock> getDataBlocks() {
		return dataBlocks;
	}

	public void setDataBlocks(List<DateBlock> dataBlocks) {
		this.dataBlocks = dataBlocks;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public MDate getNextYear() {
		return nextYear;
	}

	public void setNextYear(MDate nextYear) {
		this.nextYear = nextYear;
	}

	public MDate getNextMonth() {
		return nextMonth;
	}

	public void setNextMonth(MDate nextMonth) {
		this.nextMonth = nextMonth;
	}

	public MDate getPreYear() {
		return preYear;
	}

	public void setPreYear(MDate preYear) {
		this.preYear = preYear;
	}

	public MDate getPreMonth() {
		return preMonth;
	}

	public void setPreMonth(MDate preMonth) {
		this.preMonth = preMonth;
	}

	public ScheduleTask getTask() {
		return task;
	}

	public void setTask(ScheduleTask task) {
		this.task = task;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<ScheduleTask> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<ScheduleTask> taskList) {
		this.taskList = taskList;
	}

	public String getTaskIds() {
		return taskIds;
	}

	public void setTaskIds(String taskIds) {
		this.taskIds = taskIds;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}
}
